package at.dl.android.dota.adviser;

import java.util.List;

import android.os.Bundle;
import android.widget.EditText;
import android.widget.Spinner;

public class SearchItemActivity extends AbstractSearchActivity {

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState,
                R.layout.search_item,
                R.id.buttonSearch,
                ListItemActivity.class,
                ListItemActivity.SEARCH_ITEM_WHERE_CAUSE,
                "ITEMS",
                "name");
    }

    @Override
    protected void fillWhereElements ( List<String> whereElements ) {
        handleName(whereElements);
        handlePriceLimit(whereElements);
        handleOrb(whereElements);
        handleCheckBoxes(whereElements);
    }
    
    private void handleName ( List<String> whereElements ) {
        EditText et = (EditText) findViewById(R.id.editName);
        String searchName = et.getText().toString().trim();
        if ( searchName.length() <= 0 )
            return;
        if ( searchName.charAt(searchName.length()-1) != '%' )
            searchName += '%';
        whereElements.add(ColumnNames.Items.name + " like '" + searchName + "'");        
    }

    private void handlePriceLimit ( List<String> whereElements ) {
        EditText et = (EditText) findViewById(R.id.editPriceLimit);
        String val = et.getText().toString().trim();
        if ( val.length() <= 0 )
            return;
        whereElements.add(ColumnNames.Items.total_costs + " <= " + val);        
    }

    private void handleOrb ( List<String> whereElements ) {
        Spinner sp = (Spinner) findViewById(R.id.spinnerOrbEffects);
        String sel = sp.getSelectedItem().toString();
        if ( "(all)".equals(sel) )
            return;
        else if ( "(non)".equals(sel) )
            whereElements.add(ColumnNames.Items.orb_effect + " IS NULL");
        else if ( "(only)".equals(sel) )
            whereElements.add(ColumnNames.Items.orb_effect + " NOT NULL");
        else
            whereElements.add(ColumnNames.Items.orb_effect + " = '" + sel + "'");        
    }

    private void handleCheckBoxes ( List<String> whereElements ) {
        addFromCheckBox(whereElements,ColumnNames.Items.strength,R.id.checkBoxStrength);
        addFromCheckBox(whereElements,ColumnNames.Items.agility,R.id.checkBoxAgility);
        addFromCheckBox(whereElements,ColumnNames.Items.intelligence,R.id.checkBoxIntelligence);
        addFromCheckBox(whereElements,ColumnNames.Items.health,R.id.checkBoxHealth);
        addFromCheckBox(whereElements,ColumnNames.Items.mana,R.id.checkBoxMana);
        addFromCheckBox(whereElements,ColumnNames.Items.damage,R.id.checkBoxDamage);
        addFromCheckBox(whereElements,ColumnNames.Items.health_reg,R.id.checkBoxHealthReg);
        addFromCheckBox(whereElements,ColumnNames.Items.mana_reg,R.id.checkBoxManaReg);
        addFromCheckBox(whereElements,ColumnNames.Items.armor,R.id.checkBoxArmor);
        addFromCheckBox(whereElements,ColumnNames.Items.evasion,R.id.checkBoxEvasion);
        addFromCheckBox(whereElements,ColumnNames.Items.magic_resistance,R.id.checkBoxMagicResistance);
        addFromCheckBox(whereElements,ColumnNames.Items.attack_speed,R.id.checkBoxAttackSpeed);
        addFromCheckBox(whereElements,ColumnNames.Items.movement_speed,R.id.checkBoxMovementSpeed);
        addFromCheckBox(whereElements,ColumnNames.Items.aura,R.id.checkBoxAura);
        addFromCheckBox(whereElements,ColumnNames.Items.active_feature,R.id.checkBoxActiveFeature);
        addFromCheckBox(whereElements,ColumnNames.Items.charges,R.id.checkBoxCharges);
        addFromCheckBox(whereElements,ColumnNames.Items.passive_feature,R.id.checkBoxPassiveFeature);
    }

}
